﻿//////////////////////////////////////////////////////////////////
//
// Global.cs
//
// Copyright (c) 2012 Dan Pike. All rights reserved
//
// Refer to license.txt for any license restrictions.
// 
using Logger4Net;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace Utilities
{
   public class Global
   {
      public enum NormalizeType
      {
         None,
         City,
         Material,
         Factory,
      }

      ////////////////////////////////////////////////////////////////////////
      //
      static Global()
      {
         translationsCity_.Add("Lübeck", "Lubeck");
         translationsCity_.Add("Malmö", "Malmo");
         translationsMaterial_.Add("Raw metal", "Ingot");
         translationsMaterial_.Add("Metal goods", "Tools");
         translationsMaterial_.Add("Stockfish", "Fish");
         translationsMaterial_.Add("Brick", "Bricks");
         translationsFactory_.Add("Wood", "Sawmill");
         translationsFactory_.Add("Salter", "Saltworks");
         translationsFactory_.Add("Smith", "Toolworks");
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static string normalizeName(string name, NormalizeType objectType)
      {
         try
         {
            string translation;
            Dictionary<string, string> translator =
               (objectType == NormalizeType.City) ? translationsCity_ :
               (objectType == NormalizeType.Material) ? translationsMaterial_ :
               (objectType == NormalizeType.Factory) ? translationsFactory_ :
               null;
            if ((null != translator) && translator.TryGetValue(name, out translation))
            {
               return translation;
            }
            return cultureUSA_.TextInfo.ToTitleCase(name.Trim().ToLower());
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return null;
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static string normalizePath(string name, bool optimize)
      {
         return normalizePath(name, Directory.GetCurrentDirectory(), optimize);
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static string normalizePath(string name, string directory, bool optimize)
      {
         try
         {
            // Clean up the arguments
            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(directory))
            {
               return string.Empty;
            }
            name = name.Trim();
            directory = directory.Trim();

            // Convert the arguments to a normalized format
            name = Path.GetFullPath(name);
            directory = Path.GetFullPath(directory).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            if (optimize)
            {
               char lastChar = directory[directory.Length - 1];
               if (Path.DirectorySeparatorChar != lastChar)
               {
                  directory += Path.DirectorySeparatorChar;
               }

               if (name.StartsWith(directory))
               {
                  // It's a subdirectory of the supplied directory
                  return name.Substring(directory.Length);
               }
               string nameDirectory = Path.GetDirectoryName(name);
               if (directory.StartsWith(nameDirectory))
               {
                  // It's in a parent directory
                  string[] subFolders = directory.Substring(nameDirectory.Length).Split(
                     new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                  StringBuilder sb = new StringBuilder();
                  for (int index = 0; index < subFolders.Length; ++index)
                  {
                     sb.Append(@"..\");
                  }
                  sb.Append(Path.GetFileName(name));
                  return sb.ToString();
               }
            }
            return name;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return string.Empty;
      }

      //////////////////////////////////////////////////////////////////////////
      //
      public static string escapedString(int number)
      {
         try
         {
            StringBuilder sb = new StringBuilder();
            number.ToString("#,0").ToList().ForEach(ch => sb.Append('\\').Append(ch));
            return sb.ToString();
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return string.Empty;
      }

      ////////////////////////////////////////////////////////////////////////
      //
      private static Logger logger_ = new Logger(typeof(Global));
      private static Dictionary<string, string> translationsCity_ = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
      private static Dictionary<string, string> translationsFactory_ = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
      private static Dictionary<string, string> translationsMaterial_ = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
      public static CultureInfo cultureUSA_ = new CultureInfo("en-US", false);
   }
}
